import java.util.concurrent.*;

/**
 * 自定义拒绝策略
 */
public class ThreadPoolDemo11 {
    public static void main(String[] args) {
        ThreadFactory factory=new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread=new Thread(r);
                return thread;
            }
        };
        ThreadPoolExecutor executor=new ThreadPoolExecutor(2, 2, 10,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), factory, new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                //自定义拒绝策略业务实现
                System.out.println("我执行了自定义拒绝策略");
            }
        });
        for (int i = 0; i < 5; i++) {
            int finalI=i;
            executor.submit(()->{
                try {
                    Thread.sleep(100*finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"执行任务:"+finalI);
            });
        }
    }
}
